package b1.scenes
{
	import b1.controls.basic.BasicMc;
	import b1.net.*;
	import b1.players.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.text.*;

	public class SceneContainer extends Sprite
	{
		static private var instance:SceneContainer;
		public var currentScene:Sprite;
		public var previousScene:Sprite;
		public var currentBackgroundMusic:Class;
		public var previousBackgroundMusic:Class; 
		public var currentSoundChannel:SoundChannel;
		//新手导引步骤数
		private var _step:int;
		//新手导引箭头帮助
		private var arrowHelper1:ArrowHelper;
		private var arrowHelper2:ArrowHelper;
		//鼠标信息
		public var targetPosX:Number;
		public var targetPosY:Number;
		public var mouseDisableFlag:Boolean;
		//自定义鼠标
		private var customizedMouse:BasicMc;
		//调试信息
		private var debugText:TextField;
		private var debugLines:Array;
		private const debugLinesPerPage:int = 20;
		public var debugCurrentPage:int;
		//场景层
		public var sceneLayer:Sprite;
		//首层（最上层）
		public var topLayer:Sprite;
		 
		public function SceneContainer()
		{
			super();
			currentScene = null;
			previousScene = null;
			_step = -99999;
			targetPosX = -1;
			targetPosY = -1;
			mouseDisableFlag = false;
			debugLines = new Array(); 
			debugCurrentPage = 0;
			sceneLayer = new Sprite();
			topLayer = new Sprite();
			addChild(sceneLayer);
			addChild(topLayer);
		}
		
		public function showDebug(pageIndex:int = 0):void
		{
			if (debugText != null && debugText.parent != null)
			{
				debugText.parent.removeChild(debugText);
			}
			var pageNum:int = (debugLines.length - 1) / debugLinesPerPage + 1;
			debugCurrentPage = Math.max(Math.min(pageIndex, pageNum - 1), 0);
			var startLine:int = debugCurrentPage * debugLinesPerPage;
			var lineNum:int = Math.min(debugLinesPerPage, debugLines.length - startLine);
			var detail:String = "Page: " + (debugCurrentPage + 1).toString() + "/" + pageNum + "\n";
			for (var i:int = startLine; i < startLine + lineNum; i++)
			{
				detail += debugLines[i] + "\n"; 
			} 
			debugText = new TextField();
//			debugText.mouseEnabled = false;
			debugText.selectable = true;
        	debugText.autoSize = TextFieldAutoSize.LEFT;
        	debugText.wordWrap = true;
        	debugText.width = BFont.stageWidth - 50;
			debugText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			debugText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			debugText.text = detail;
			stage.addChild(debugText);
		}
		
		//显示调试信息
		public function switchDebug():void
		{
			if (debugText != null && debugText.parent != null)
			{
				debugText.parent.removeChild(debugText);
			}
			else
			{
				showDebug((debugLines.length - 1) / debugLinesPerPage);
			}
		}
		
		public function debug(detail:String):void
		{
			debugLines.push(ServerHelper.getInstance().now() + ": " + detail);
		}
		
		//检查鼠标位置
		public function checkOnlyMousePosition(currentTarget:Object):Boolean
		{
			var globalPoint:Point = currentTarget.parent.localToGlobal(new Point(currentTarget.x, currentTarget.y));
			trace("checkOnlyMousePosition:", globalPoint.x, globalPoint.y);
			if (globalPoint.x == targetPosX && globalPoint.y == targetPosY)
				return true;
			else
				return false;
		}
		
		//清空新手引导上个步骤的所有指示
		public function clearLastStep():void
		{
			if (null != arrowHelper1 && null != arrowHelper1.parent)
			{
				arrowHelper1.parent.removeChild(arrowHelper1);
				arrowHelper1 = null;
			}
			if (null != arrowHelper2 && null != arrowHelper2.parent)
			{
				arrowHelper2.parent.removeChild(arrowHelper2);
				arrowHelper2 = null;
			}
		}
		
		//获得当前新手引导的当前步骤
		public function get step():int
		{
			return _step;
		}
		
		//开始新手引导
		public function startNewHand():void
		{
			_step = 0;
			addStep();
		}
		
		//停止新手引导
		public function stopNewHand():void
		{
			clearLastStep();
			mouseDisableFlag = false;
			targetPosX = -1;
			targetPosY = -1;
			_step = -99999;
		}
		
		//新手导引步骤
		public function addStep():void
		{
			clearLastStep();
			switch(step)
			{
				case 0:
					arrowHelper1 = new ArrowHelper(1, "请选择动物", 110, 30);
					arrowHelper1.x = 226;
					arrowHelper1.y = 230;
					arrowHelper2 = new ArrowHelper(0, "请选择动物", 110, 30);
					arrowHelper2.x = 482;
					arrowHelper2.y = 380;
					currentScene.addChild(arrowHelper2);
					break;
				case 1:
					mouseDisableFlag = true;
					targetPosX = 352.2;
					targetPosY = 345.95;
					arrowHelper1 = new ArrowHelper(0, "单击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;
				case 2:
					mouseDisableFlag = true;
					targetPosX = 245.15;
					targetPosY = 550;
					arrowHelper1 = new ArrowHelper(1, "单击打开装备", 130,30);
					arrowHelper1.x = 240;
					arrowHelper1.y = 422;
					break;	
				case 3:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(1, "鼠标移动到装备上方", 130, 70);
					arrowHelper1.x = 120;
					arrowHelper1.y = 340;
					break;
				case 4:
					mouseDisableFlag = true;
					targetPosX = 120;
					targetPosY = 475;
					arrowHelper1 = new ArrowHelper(2, "点击“装备”", 160, 30);
					arrowHelper1.x = 260;
					arrowHelper1.y = 390;
					break;
				case 5:
					mouseDisableFlag = true;
					targetPosX = 245.15;
					targetPosY = 550;
					arrowHelper1 = new ArrowHelper(2, "点击确定", 90, 30);
					arrowHelper1.x = 440;
					arrowHelper1.y = 320;
					break;		
				case 6:
					mouseDisableFlag = true;
					targetPosX = 10;
					targetPosY = 200;
					arrowHelper1 = new ArrowHelper(2, "单击查看任务", 130, 30);
					arrowHelper1.x = 110;
					arrowHelper1.y = 200;
					break;
				case 7:
					mouseDisableFlag = true;
					targetPosX = 349.7;
					targetPosY = 392.3;
					arrowHelper1 = new ArrowHelper(2, "单击领取任务", 130, 30);
					arrowHelper1.x = 485;
					arrowHelper1.y = 375;
					break;
				case 8:
					mouseDisableFlag = true;
					targetPosX = 681.55;
					targetPosY = 10;
					arrowHelper1 = new ArrowHelper(0, "单击探案", 90, 30);
					arrowHelper1.x = 665;
					arrowHelper1.y = 120;
					break;
				case 9:
					mouseDisableFlag = true;
					targetPosX = 160;
					targetPosY = 107;
					arrowHelper1 = new ArrowHelper(0, "选择探案对象", 130, 30);
					arrowHelper1.x = 180;
					arrowHelper1.y = 290;
					break;
				case 10:
					mouseDisableFlag = true;
					targetPosX = 334.45;
					targetPosY = 345.95;
					arrowHelper1 = new ArrowHelper(2, "点击确定", 90, 30);
					arrowHelper1.x = 460;
					arrowHelper1.y = 320;
					break;
				case 11:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;
				case 12:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;
				case 13:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;
				case 14:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;
				case 15:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;
				case 16:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;
				case 17:
					mouseDisableFlag = true;
					arrowHelper1 = null;
					break;
				case 18:
					mouseDisableFlag = true;
					targetPosX = 80;
					targetPosY = 240;
					arrowHelper1 = new ArrowHelper(1, "点击使用骨头普通攻击", 130, 60);
					arrowHelper1.x = 71;
					arrowHelper1.y = 100;
					break;
				case 19:
					mouseDisableFlag = true;
					targetPosX = 290;
					targetPosY = 240;
					arrowHelper1 = new ArrowHelper(1, "点击召唤黄雀", 130, 60);
					arrowHelper1.x = 282;
					arrowHelper1.y = 100;
					break;
				case 20:
					mouseDisableFlag = true;
					targetPosX = 150;
					targetPosY = 240;
					arrowHelper1 = new ArrowHelper(1, "点击使用火球", 130, 60);
					arrowHelper1.x = 144;
					arrowHelper1.y = 100;
					break;	
				case 21:
					mouseDisableFlag = true;
					targetPosX = 80;
					targetPosY = 310;
					arrowHelper1 = new ArrowHelper(0, "点击使用奥义嗜血", 130, 60);
					arrowHelper1.x = 71;
					arrowHelper1.y = 425;
					break;	
				case 22:
					mouseDisableFlag = true;
					targetPosX = 105.05;
					targetPosY = 545;
					arrowHelper1 = new ArrowHelper(1, "想更厉害吗？试试道具吧！单击打开道具", 200, 60);
					arrowHelper1.x = 100;
					arrowHelper1.y = 410;
					break;
				case 23:
					mouseDisableFlag = true;
					targetPosX = 97;
					targetPosY = 527;
					arrowHelper1 = new ArrowHelper(2, "单击选道具", 110, 30);
					arrowHelper1.x = 190;
					arrowHelper1.y = 520;
					break;
				case 24:
					arrowHelper1 = new ArrowHelper(1, "单击对象，使用道具", 200, 30);
					arrowHelper1.x = 556;
					arrowHelper1.y = 300;
					break;
				case 25:
					mouseDisableFlag = true;
					targetPosX = 15;
					targetPosY = 300;
					arrowHelper1 = new ArrowHelper(2, "单击启用托管", 130, 30);
					arrowHelper1.x = 100;
					arrowHelper1.y = 280;
					break;	
				case 26:
					mouseDisableFlag = true;
					arrowHelper1 = null;
					break;
				case 27:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(2, "点击确定, 结束战斗", 90, 60);
					arrowHelper1.x = 500;;
					arrowHelper1.y = 350;
					break;
				case 28:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;	
				case 29:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;	
				case 30:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;	
				case 31:
					mouseDisableFlag = true;
					arrowHelper1 = new ArrowHelper(0, "点击确定", 90, 30);
					arrowHelper1.x = 360;
					arrowHelper1.y = 422;
					break;	
				case 32:
					mouseDisableFlag = true;
					targetPosX = 349.7;
					targetPosY = 392.3;
					arrowHelper1 = new ArrowHelper(2, "点击分享", 90, 30);
					arrowHelper1.x = 485;
					arrowHelper1.y = 375;
					break;	
				case 33:
					stopNewHand();
					break;	
				default:
					break;
			} 
			if (null != arrowHelper1)
			{
				this.currentScene.addChild(arrowHelper1);
			} 
			if (step >= 0)
				_step++;
		}
		
		static public function getInstance():SceneContainer
		{
			if (!instance)
				instance = new SceneContainer();
			return instance;
		}
		
		public function loadScene(scene:Sprite, BackgroundMusic:Class = null):void
		{
			if (!scene)
				return;
			if (currentScene != scene)
			{
				unloadScene();
				sceneLayer.addChild(scene);
				currentScene = scene;
				currentBackgroundMusic = BackgroundMusic;
				if (BackgroundMusic != null)
				{			
					var sound:Sound = new BackgroundMusic();
					currentSoundChannel = sound.play();
				}
			} 
		}
		
		public function unloadScene():void
		{
			if (currentScene)
			{
				sceneLayer.removeChild(currentScene);
				previousScene = currentScene;
				previousBackgroundMusic = currentBackgroundMusic;
				if (currentSoundChannel != null)
					currentSoundChannel.stop();
			}
		}
		
		public function loadPreviousScene():void
		{
			loadScene(previousScene, previousBackgroundMusic);
		}
		
		//使用自定义鼠标
		public function customizeMouse():void
		{
//			Mouse.hide();
			if (customizedMouse == null)
			{
				var SandGlass:Class = ResourceHelper.getInstance().getClassByName("SandGlass");
				if (SandGlass != null)
				{
					customizedMouse = new BasicMc(SandGlass);
					customizedMouse.mouseEnabled = false;
					customizedMouse.x = this.mouseX;
					customizedMouse.y = this.mouseY;
					stage.addChild(customizedMouse);
					stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				}
			}
		}
		
		private function onMouseMove(event:MouseEvent):void
		{
			customizedMouse.x = this.mouseX;
			customizedMouse.y = this.mouseY;
			event.updateAfterEvent();
		}
		
		//还原系统鼠标		
		public function restoreSystemMouse():void
		{
//			Mouse.show();
			if (customizedMouse != null)
			{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				stage.removeChild(customizedMouse);
				customizedMouse = null;
			}
		}
	}
}